import 'package:aihu_getx_app/app/modules/admin_login/providers/admin_login_provider.dart';
import 'package:aihu_getx_app/app/modules/d/d_home/controllers/d_home_controller.dart';
import 'package:aihu_getx_app/app/modules/d/d_my/models/user_profile_resp_vo.dart';
import 'package:aihu_getx_app/app/modules/d/d_my/providers/d_my_provider.dart';
import 'package:aihu_getx_app/app/routes/app_pages.dart';
import 'package:aihu_getx_app/utils/cache_keys.dart';
import 'package:get/get.dart';
import 'package:sp_util/sp_util.dart';

class DMyController extends DHomeController {
  final String title = "个人中心";
  final count = 0.obs;
  final userProfile = UserProfileRespVO().obs;
  final loading = false.obs;

  Future onRefresh() {
    if (loading.isTrue) {
      return Future.value();
    }
    onLoading();
    return Future.value();
  }

  void onLoading() async {
    loading.value = true;
    try {
      var res = await DMyProvider.getUserInfo();
      userProfile.value = res;
    } finally {
      loading.value = false;
    }
  }

  @override
  void onInit() {
    super.onInit();
  }

  @override
  void onReady() {
    super.onReady();
    onLoading();
  }

  @override
  void onClose() {
    super.onClose();
  }

  void increment() => count.value++;

  void logout() {
    AdminLoginProvider.logout().then(
      (value) => {
        SpUtil.remove(CacheKeys.dLastPageIndex),
        Get.offAllNamed(Routes.dHome),
      },
      onError: (err) {
        SpUtil.remove(CacheKeys.dLastPageIndex);
        Get.offAllNamed(Routes.dHome);
      },
    );
  }
}
